package com.sleepsocial.web.authentication;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.sleepsocial.authentication.AuthenticationManager;
import com.sleepsocial.web.Web;

public class WebAuthenticatorInterceptor extends HandlerInterceptorAdapter {
    
    public boolean isWebRequest(HttpServletRequest request) {
        return request.getServletPath().startsWith(Web.WEB_BASE_URL);
    }
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (isWebRequest(request)) {
            if (AuthenticationManager.isUserAuthenticated(request)) {
                return true;
            } else {
                response.sendRedirect(WebAuthenticationController.LOGIN_URL);
                return false;
            }
        } else {
            return super.preHandle(request, response, handler);
        }
    }
    
    
}